<template>
  编辑招聘信息
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="职位名称">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="工作地点">
      <el-cascader
        v-model="form.address"
        placeholder="试试搜索：北京"
        :options="options"
        :props="{ checkStrictly: true }"
        @change="gaibianaddress"
        filterable
      ></el-cascader>
    </el-form-item>
    <el-form-item label="学历限制">
      <el-radio-group v-model="form.xueli">
        <el-radio :label="1">不限学历</el-radio>
        <el-radio :label="2">小学</el-radio>
        <el-radio :label="3">初中</el-radio>
        <el-radio :label="4">高中/中专</el-radio>
        <el-radio :label="5">大专</el-radio>
        <el-radio :label="6">本科</el-radio>
        <el-radio :label="7">硕士</el-radio>
        <el-radio :label="8">博士</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="招聘人数">
      <el-input-number
        v-model="form.zhaopinnum"
        @change="handleChange"
        :min="1"
        :max="10"
        label="描述文字"
      ></el-input-number>
    </el-form-item>
    <el-form-item label="工作经验">
      <el-input v-model="form.jingyan" placeholder="请输入内容"></el-input>
    </el-form-item>
    <el-form-item label="职位要求">
      <el-input type="textarea" v-model="form.desc" :rows="10"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即修改</el-button>
      <el-button @click="quxiao">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import province from "../../static/json/province.json";
import city from "../../static/json/city.json";
import area from "../../static/json/area.json";
export default {
  props:['jwttoken'],
  name: "EditZhaopin",
  data() {
    return {
      form: {},
      options: [],
      id:0
    };
  },
  created: function () {
    let id = this.$route.params.id;
    this.id=id
    this.getZhaoPinInfo(id);
    this.setarea();
  },
  methods: {
    getZhaoPinInfo: function (id) {
      let that = this;
      this.$jquery.ajax({
        async: false,
        type: "GET",
        dataType: "json",
        url: this.$baseurl + "getZhaoPinInfo/" + id,
        beforeSend: function (request) {
          request.setRequestHeader("Authorization", that.jwttoken);
        },
        success: function (rtn) {
          if (rtn.code == -5) {
            that.$emit("loginstate1", -1);
          }
          if (rtn.code == 1) {
            that.form = rtn.data;
            that.form.address = rtn.data.address.split("-");
          }
        },
      });
    },
    gaibianaddress: function (evt) {
      let address = "";
      for (let c in evt) {
        address = address + evt[c] + "-";
      }
      address = address.substr(0, address.length - 1);
      this.form.address = address;
    },
    setarea: function () {
      this.options = province;
      for (let i in province) {
        province[i]["label"] = province[i]["name"];
        province[i]["value"] = province[i]["name"];
        province[i].children = [];
        for (let i1 in city[province[i]["id"]]) {
          let city1 = {};
          city1.label = city[province[i]["id"]][i1].name;
          city1.value = city[province[i]["id"]][i1].name;
          city1.children = [];
          for (let i2 in area[city[province[i]["id"]][i1].id]) {
            let id2 = area[city[province[i]["id"]][i1].id];
            let area1 = {};
            area1.label = id2[i2].name;
            area1.value = id2[i2].name;
            city1.children.push(area1);
          }
          province[i].children.push(city1);
        }
      }
      console.log(this.options);
    },
    onSubmit: function () {
      if (this.form.name == "") {
        this.$notify({
          title: "警告",
          message: "请输入职位名称",
          type: "warning",
        });
        return false;
      }
      if (this.form.address == "") {
        this.$notify({
          title: "警告",
          message: "请选择工作地点",
          type: "warning",
        });
        return false;
      }
      if (this.form.desc == "") {
        this.$notify({
          title: "警告",
          message: "请填写职位描述",
          type: "warning",
        });
        return false;
      }
      if (this.form.jingyan == "") {
        this.$notify({
          title: "警告",
          message: "请填写经验",
          type: "warning",
        });
        return false;
      }
      let that = this;
      if(this.form.address instanceof Array){
          this.form.address=this.form.address.join("-")
      }
      this.$jquery.ajax({
        async: false,
        type: "POST",
        dataType: "json",
        url: this.$baseurl + "updateZhaoPin",
        beforeSend: function (request) {
          request.setRequestHeader("Authorization", that.jwttoken);
        },
        data: {
          id: this.id,
          name: this.form.name,
          address: this.form.address,
          xueli: this.form.xueli,
          zhaopinnum: this.form.zhaopinnum,
          jingyan: this.form.jingyan,
          desc: this.form.desc,
        },
        success: function (rtn) {
          if (rtn.code == -5) {
            that.$emit("loginstate1", -1);
          }
          if (rtn.code == 1) {
            that.$notify({
              title: "成功",
              message: "修改成功",
              type: "success",
            });
            setTimeout(() => {
              that.$router.push("/ZhaoPinList");
            }, 2000);
          } else {
            that.$notify.info({
              title: "失败",
              message: "修改失败",
            });
          }
        },
      });
    },
  },
};
</script>
